package org.bot.plugin.web.event;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.bot.api.event.DataStorage;
import org.bot.api.event.EventName;
import org.bot.api.event.Events;
import org.bot.api.flow.FlowNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 沧月
 * @create 0020 2023/9/20 12:52
 * @description <description class purpose>
 */
@Component
@EventName("批量获取值")
public class WebBatchGetValue implements Events {

    @Autowired
    WebGetValue webGetValue;

    @Override
    public Object exec(FlowNode note, DataStorage data) {
        String arg = note.getArg();
        JSONArray array = JSONArray.parseArray(arg);
        Map<String, Object> _dt = new HashMap<>();
        FlowNode flowNode = new FlowNode();
        array.forEach(o -> {
            JSONObject _tmp = (JSONObject) o;
            flowNode.setDriveWeb(note.getDriveWeb());
            flowNode.setArg(_tmp.getString("arg"));
            flowNode.setSelectorType(note.getSelectorType());
            Object value = webGetValue.exec(flowNode, data);
            _dt.put(_tmp.getString("key"), value);
        });
        return _dt;
    }
}
